package com.pactera.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pactera.entity.QUserBean;
import com.pactera.entity.UserBean;
import com.pactera.repository.UserJPA;
import com.querydsl.jpa.impl.JPAQueryFactory;

@RestController
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	private UserJPA userJPA;

	// 实体管理者
	@Autowired
	private EntityManager entityManager;

	// JPA查询工厂
	private JPAQueryFactory queryFactory;

	// 通过@PostConstruct注解在类初始化的时候完成对JPAQueryFactory对象的实例化
	@PostConstruct
	public void initFactory() {
		queryFactory = new JPAQueryFactory(entityManager);
		System.out.println("init JPAQueryFactory successfully");
	}

	@RequestMapping(value = "/queryAll")
	public List<UserBean> queryAll() {
		// 使用querydsl查询
		QUserBean _Q_user = QUserBean.userBean;
		// 查询并返回结果集
		return queryFactory.selectFrom(_Q_user)// 查询源
				.orderBy(_Q_user.id.desc())// 根据id倒序
				.fetch();// 执行查询并获取结果集
	}

	@RequestMapping(value = "/detail/{id}")
	public UserBean detail(@PathVariable("id") Long id) {
		// 使用querydsl查询
		QUserBean _Q_user = QUserBean.userBean;
		// 查询并返回结果集
		return queryFactory.selectFrom(_Q_user)// 查询源
				.where(_Q_user.id.eq(id))// 指定查询具体id的数据
				.fetchOne();// 执行查询并返回单个结果
	}

	/**
	 * QueryDSL 实现数据查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/detail_2/{id}")
	public UserBean detail_2(@PathVariable("id") Long id) {
		// 使用querydsl查询
		QUserBean _Q_user = QUserBean.userBean;
		// 查询并返回指定id的单条数据
		return userJPA.findOne(_Q_user.id.eq(id));
	}

	@RequestMapping(value = "/likeQueryWithName")
	public List<UserBean> likeQueryWithName(String name) {
		// 使用querydsl查询
		QUserBean _Q_user = QUserBean.userBean;
		return queryFactory.selectFrom(_Q_user)// 查询源
				.where(_Q_user.name.like(name + "%"))// 根据name模糊查询
				.fetch();// 执行查询并返回结果集
	}

	/**
	 * 使用Jpa更新会员信息
	 * 
	 * @param userBean
	 **/
	@RequestMapping(value = "/updateWithJpa")
	public String updateWithJpa(UserBean userBean) {
		// 保存会员信息相当于Hibernate内的SaveOrUpdate
		UserBean ub = userJPA.save(userBean);
		if (ub != null) {
			System.err.println(ub.getAddress());
		}
		return "SUCCESS";
	}

	@Transactional
	@RequestMapping(value = "/updateWithQueryDsl")
	public String updateWithQueryDsl(UserBean userBean) {
		// querydsl查询实体
		QUserBean _Q_user = QUserBean.userBean;
		queryFactory.update(_Q_user)// 更新对象
				// 更新字段列表
				.set(_Q_user.name, userBean.getName()).set(_Q_user.address, userBean.getAddress())
				.set(_Q_user.age, userBean.getAge()).set(_Q_user.pwd, userBean.getPwd())
				// 更新条件
				.where(_Q_user.id.eq(userBean.getId()))
				// 执行更新
				.execute();
		return "SUCCESS";
	}

	@RequestMapping(value = "/deleteWithJpa")
	public String deleteWithJpa(UserBean userBean) {
		// 执行删除指定主键的值
		userJPA.delete(userBean.getId());
		return "SUCCESS";
	}

	@RequestMapping(value = "/deleteWithQueryDsl")
	@Transactional
	public String deleteWithQueryDsl(UserBean userBean) {
		// querydsl查询实体
		QUserBean _Q_user = QUserBean.userBean;
		queryFactory // 删除对象
				.delete(_Q_user) // 删除条件
				.where(_Q_user.id.eq(userBean.getId())) // 执行删除
				.execute();
		return "SUCCESS";
	}

	@RequestMapping(value = "/deleteWithQueryDsl_2")
	@Transactional
	public String deleteWithQueryDsl_2(UserBean userBean) {
		// querydsl查询实体
		QUserBean _Q_user = QUserBean.userBean;
		queryFactory // 删除对象
				.delete(_Q_user) // 删除条件
				.where(_Q_user.name.eq(userBean.getName()).and(_Q_user.age.gt(20)))
				.execute();// 执行删除
		return "SUCCESS";
	}

}
